package com.bhs.service.carousel.impl;

import com.bhs.pojo.Carousel;
import com.bhs.redis.Redis;
import com.bhs.service.carousel.CarouselService;
import com.bhs.mappers.carousel.CarouselMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarouselServiceImpl implements CarouselService {
    @Autowired
    private CarouselMapper carouselMapper;

    @Override
    public List<Carousel> findCarousel() {
        if(Redis.redisJedis().exists("carousel")){
            String carousel = Redis.redisJedis().get("carousel");
            List<Carousel> list = null;
            try {
                list = new ObjectMapper().readValue(carousel, List.class);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            return list;
        }
        List<Carousel> carouselList = carouselMapper.findCarousel();
        try {
            String string1 = new ObjectMapper().writeValueAsString(carouselList);
            Redis.redisJedis().set("carousel", string1);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return carouselList;
    }
}
